﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SearchOperations.DAL;
using SearchOperations;
using System.Collections.Generic;
using System.ComponentModel;

namespace ClientSide
{
    public partial class frmComplexReservationsaspx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddStartPoint.DataSource = BookOperations.GetAllSettlements(true);
                ddStartPoint.DataTextField = "SettlementName";
                ddStartPoint.DataValueField = "SettlementID";
                ddStartPoint.DataBind();
                ddEndPoint.DataSource = BookOperations.GetAllSettlements(true);
                ddEndPoint.DataTextField = "SettlementName";
                ddEndPoint.DataValueField = "SettlementID";
                ddEndPoint.DataBind();


                ddChooseBy.DataSource = new List<string>() { "Price", "Time" };
                ddChooseBy.DataBind();

                panelDetails.Visible = false;
                lblNote.Visible = false;
                panelPass.Visible = false;
                lblBookNote.Visible = false;
            }
            if (Session["type"] != null && Session["type"].ToString() == "bookCompl")
            {
                ddStartPoint.SelectedIndex = (int)(Session["startI"]);
                ddEndPoint.SelectedIndex = (int)(Session["endI"]);
                tbDate.Text = Session["date"].ToString();
                ddChooseBy.SelectedIndex = (int)Session["sortI"];
                RepeaterDataBound(Session["date"].ToString(), Session["start"].ToString(), Session["end"].ToString(),
                    Session["sort"].ToString()=="Price");
            }

            btnShow.Click += new EventHandler(btnShow_Click);
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            outerRep.Visible = true;
            panelDetails.Visible = false;
            lblNote.Visible = false;
            panelPass.Visible = false;
            lblBookNote.Visible = false;

            bool isPrice = (ddChooseBy.SelectedIndex == 0);
            if (ddStartPoint.SelectedValue != "0" & ddEndPoint.SelectedValue != "0")
            {
                if (ddStartPoint.SelectedValue != ddEndPoint.SelectedValue)
                {
                    lblRequired.Text = string.Empty;
                    string date = tbDate.Text;

                    RepeaterDataBound(date, ddStartPoint.SelectedValue, ddEndPoint.SelectedValue, isPrice);
                }
                else
                {
                    lblRequired.Text = "Settlements supposed to be different!";
                }
            }
            else
            {
                lblRequired.Text = "Settlement required!";
            }
        }

        private void RepeaterDataBound(string date, string start, string end, bool isPrice)
        {
            string[] dateArr = date.Split('/');
            DateTime tDate = new DateTime(int.Parse(dateArr[2]),
                int.Parse(dateArr[0]), int.Parse(dateArr[1]));

            List<Route> list = DijkstraSearching.GetAllRoutes(tDate, long.Parse(start),
                long.Parse(end), isPrice);

            if (list.Count == 0)
            {
                lblBookNote.Text = "Currently we do not offer such route.";
                lblBookNote.Visible = true;
            }

            outerRep.DataSource = list;
            outerRep.ItemDataBound += new RepeaterItemEventHandler(outerRep_ItemDataBound);
            outerRep.DataBind();
        }

        protected void outerRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            Repeater inner = e.Item.FindControl("innerRep") as Repeater;

            inner.DataSource = DataBinder.Eval(e.Item.DataItem, "DisplayInfo");
            inner.DataBind();
        }

        protected void BtnBook_Command(object sender, CommandEventArgs e)
        {
            if (User.Identity.Name != "")
            {
                panelDetails.Visible = false;
                lblNote.Visible = false;
                tbPassengers.Text = string.Empty;
                panelPass.Visible = true;
                Session["complexBook"] = e.CommandArgument;
            }
            else
            {
                Session["type"] = "bookCompl";
                Session["start"] = ddStartPoint.SelectedValue;
                Session["startI"] = ddStartPoint.SelectedIndex;
                Session["end"] = ddEndPoint.SelectedValue;
                Session["endI"] = ddEndPoint.SelectedIndex;
                Session["date"] = tbDate.Text;
                Session["sort"] = ddChooseBy.SelectedValue;
                Session["sortI"] = ddChooseBy.SelectedIndex;
                Response.Redirect("frmLogin1.aspx");
            }
        }

        protected void BtnConfirm_Command(object sender, CommandEventArgs e)
        {
            Session["type"] = null;
            string passengers = tbPassengers.Text;
            string details = Session["complexBook"].ToString();
            lblBookNote.Text = BookOperations.ComplexBook(details, int.Parse(tbPassengers.Text), User.Identity.Name);
            lblBookNote.Visible = true;
            lblNote.Visible = false;
            panelDetails.Visible = false;


        }

        protected void BtnRoadSegment_Command(object sender, CommandEventArgs e)
        {
            panelDetails.Visible = true;
            panelPass.Visible = false;
            lblBookNote.Visible = false;

            string[] args = e.CommandArgument.ToString().Split(',');
            string[] roadPoints = args[1].Split('-');
            string destinationName = args[0].Split(':')[1].Trim();
            long startID = BookOperations.GetSettlementID(roadPoints[0]);
            long endID = BookOperations.GetSettlementID(roadPoints[roadPoints.Length - 1]);

            DeatailsShow(destinationName, startID, endID);
            lblDestName.Text = "Detination: " + args[0];
            lblNote.Visible = true;
        }

        private void DeatailsShow(string destinationName, long startPointID, long endPointID)
        {
            List<RouteDetails> rdList = BookOperations.GetRouteDetails(destinationName, startPointID, endPointID);

            repDetails.DataSource = rdList;
            repDetails.DataBind();
        }

        protected void BtnCancel_comand(object sender, CommandEventArgs e)
        {
            Session["type"] = null;
            panelDetails.Visible = false;
            panelPass.Visible = false;
            lblBookNote.Visible = false;
            lblNote.Visible = false;
            outerRep.Visible = false;
        }
    }
}
